﻿Imports System.Drawing


Namespace Forms.ImageForm
    ''' <summary>
    ''' 自定义窗体的父类，这里实现任意位置的移动
    ''' Author : ajayumi
    ''' Date   : 2010-10-19
    ''' </summary>
    Public Class FormBase
        Inherits System.Windows.Forms.Form

#Region "variables"

        Private m_MouseDownPosition As Point = Nothing      '鼠标按下时的坐标
        Private m_IsMouseDown As Boolean = False            '录鼠标按键是否按下
        Private m_MouseDownFormLocation As Point = Nothing  '鼠标按下时窗体的位置

#End Region

#Region "properties"

        Private m_AllowDrag As Boolean = True
        ''' <summary>
        ''' 是否允许拖动窗体
        ''' </summary>
        Public Property AllowDrag() As Boolean
            Get
                Return m_AllowDrag
            End Get
            Set(ByVal value As Boolean)
                m_AllowDrag = value
            End Set
        End Property

#End Region


        ''' <summary>
        ''' 鼠标按下事件，可以重写该事件
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Protected Overridable Sub Form_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
            Dim _Point As Drawing.Point = Me.PointToScreen(e.Location)
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Me.m_MouseDownPosition = New Point(e.X, e.Y)
                Me.m_MouseDownFormLocation = Me.Location
                Me.m_IsMouseDown = True
            ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
                'TODO:按下鼠标右键
            End If
        End Sub

        ''' <summary>
        ''' 鼠标弹起事件，可以重写该事件
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Protected Overridable Sub Form_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
            Me.m_IsMouseDown = False
        End Sub

        ''' <summary>
        ''' 鼠标移动事件，可以重写该事件
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Protected Overridable Sub Form_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
            If Me.m_IsMouseDown = True AndAlso Me.m_AllowDrag = True Then
                Dim _Point As Drawing.Point = MousePosition
                '获得鼠标偏移量
                _Point.Offset(-Me.m_MouseDownPosition.X, -Me.m_MouseDownPosition.Y)
                '设置窗体随鼠标一起移动
                Me.Location = _Point

                Me.MouseMoveAttachWork(Me.Left - Me.m_MouseDownFormLocation.X, Me.Top - Me.m_MouseDownFormLocation.Y)
                Me.m_MouseDownFormLocation = Me.Location
            End If
        End Sub

        ''' <summary>
        ''' 鼠标移动的附带方法
        ''' </summary>
        ''' <param name="offsetX">x偏移量</param>
        ''' <param name="offsetY">y偏移量</param>
        Protected Overridable Sub MouseMoveAttachWork(ByVal offsetX As Integer, ByVal offsetY As Integer)
            'TODO: 鼠标移动的扩展方法
        End Sub

    End Class

End Namespace